﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace EasyComm.Client
{
    public partial class RegisterUserForm : Form
    {
        public RegisterUserForm()
        {
            InitializeComponent();
        }

        private bool bUsernameOK = false;
        private bool bPasswordOK = false;
        private bool bFirstNameOK = false;
        private bool bEmailOK = false;
        private bool bPasswordMatched = false;
        private bool bEnableRegisterBtn
        {
            get
            {
                if (bUsernameOK && bPasswordOK && bEmailOK && bFirstNameOK && bPasswordMatched)
                    return true;
                else
                    return false;
            }
            set
            {
                if (bUsernameOK && bPasswordOK && bEmailOK && bFirstNameOK && bPasswordMatched)
                    value = true;
                else
                    value = false;
            }
        }

        private void btnResetRegForm_Click(object sender, EventArgs e)
        {
            txtBoxFirstNameRegForm.ResetText();
            txtBoxLastNameRegForm.ResetText();
            txtBoxUserNameRegForm.ResetText();
            txtBoxPasswordRegForm.ResetText();
            txtBoxRetypePasswordRegForm.ResetText();
            txtBoxEmailRegForm.ResetText();
            txtBoxPhoneRegForm.ResetText();
        }

        private void btnCancelRegForm_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnRegisterRegForm_Click(object sender, EventArgs e)
        {

        }

        private void txtBoxFirstNameRegForm_Validating(object sender, CancelEventArgs e)
        {
            ValidateFirstNameField();
        }

        private void txtBoxFirstNameRegForm_TextChanged(object sender, EventArgs e)
        {
            ValidateFirstNameField();
        }

        private void txtBoxUserNameRegForm_TextChanged(object sender, EventArgs e)
        {
            ValidateUserNameField();
        }

        private void txtBoxUserNameRegForm_Validating(object sender, CancelEventArgs e)
        {
            ValidateUserNameField();
        }

        private void txtBoxPasswordRegForm_TextChanged(object sender, EventArgs e)
        {
            ValidatePasswordField();
        }

        private void txtBoxPasswordRegForm_Validating(object sender, CancelEventArgs e)
        {
            ValidatePasswordField();
        }

        private void txtBoxRetypePasswordRegForm_TextChanged(object sender, EventArgs e)
        {
            ValidatePasswordMatch();
        }

        private void txtBoxRetypePasswordRegForm_Validating(object sender, CancelEventArgs e)
        {
            ValidatePasswordMatch();
        }

        private void txtBoxEmailRegForm_TextChanged(object sender, EventArgs e)
        {
            ValidateEmailField();
        }

        private void txtBoxEmailRegForm_Validating(object sender, CancelEventArgs e)
        {
            ValidateEmailField();
        }

        private void ValidateFirstNameField()
        {
            if (txtBoxFirstNameRegForm.Text == "")
            {
                errProRegForm.SetError(txtBoxFirstNameRegForm, "Firstname cannot be empty");
                bFirstNameOK = false;
            }
            else
            {
                errProRegForm.SetError(txtBoxFirstNameRegForm, "");
                bFirstNameOK = true;
            }

            btnRegisterRegForm.Enabled = bEnableRegisterBtn;
        }
        
        private void ValidateUserNameField()
        {
            if (txtBoxUserNameRegForm.Text == "")
            {
                errProRegForm.SetError(txtBoxUserNameRegForm, "Username cannot be empty");
                bUsernameOK = false;
            }
            else
            {
                errProRegForm.SetError(txtBoxUserNameRegForm, "");
                bUsernameOK = true;
            }

            btnRegisterRegForm.Enabled = bEnableRegisterBtn;
        }

        private void ValidateEmailField()
        {
            if (txtBoxEmailRegForm.Text == "")
            {
                errProRegForm.SetError(txtBoxEmailRegForm, "Email cannot be empty");
                bEmailOK = false;
            }
            else
            {
                if (isEmail(txtBoxEmailRegForm.Text))
                {
                    errProRegForm.SetError(txtBoxEmailRegForm, "");
                    bEmailOK = true;
                }
                else
                {
                    errProRegForm.SetError(txtBoxEmailRegForm, "Invalid email address");
                    bEmailOK = false;
                }
            }

            btnRegisterRegForm.Enabled = bEnableRegisterBtn;
        }

        private void ValidatePasswordField()
        {
            if (txtBoxPasswordRegForm.Text == "")
            {
                errProRegForm.SetError(txtBoxPasswordRegForm, "Password cannot be empty");
                bPasswordOK = false;
            }
            else
            {
                errProRegForm.SetError(txtBoxPasswordRegForm, "");
                bPasswordOK = true;
            }

            btnRegisterRegForm.Enabled = bEnableRegisterBtn;
        }

        private void ValidatePasswordMatch()
        {
            if (txtBoxRetypePasswordRegForm.Text == "")
            {
                errProRegForm.SetError(txtBoxRetypePasswordRegForm, "Re-type the password");
                bPasswordMatched = false;
            }
            else
            {
                errProRegForm.SetError(txtBoxRetypePasswordRegForm, "");
                if (txtBoxPasswordRegForm.Text == txtBoxRetypePasswordRegForm.Text)
                {
                    bPasswordMatched = true;
                }
                else
                {
                    bPasswordMatched = false;
                    errProRegForm.SetError(txtBoxRetypePasswordRegForm, "password not mached");
                }
            }

            btnRegisterRegForm.Enabled = bEnableRegisterBtn;
        }

        private bool isEmail(string inputEmail)
        {
            Regex re = new Regex(@"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$",
                          RegexOptions.IgnoreCase);
            return re.IsMatch(inputEmail);
        }

        private void RegisterUserForm_Load(object sender, EventArgs e)
        {
            btnRegisterRegForm.Enabled = bEnableRegisterBtn;
        }
    }
}
